package algorithm.search;

public class Sqt {
    private static final double PRECISE = 0.000001;

    public double calc(double num) {
        double low = 0;
        double high = num;
        if (num < 1 && num > 0) {
            low = num;
            high = 1;
        }

        double mid = low + (high - low) / 2;
        while (high - low > PRECISE) {
            if (mid * mid > num) {
                high = mid - PRECISE;
            }
            else if (mid * mid < num) {
                low = mid + PRECISE;
            }
            else {
                return mid;
            }
            mid = low + (high - low) / 2;
        }
        return mid;
    }
}
